<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path" v-if='item.meta.title'>
        <!-- <span to="/user/notice/index">{{item.meta.title}}</span> -->
        <span v-if='item.redirect==="noredirect"||index==levelList.length-1' class="no-redirect">{{item.meta.title}}</span>
        <router-link v-else :to="item.redirect||item.path" @click.native="setToRouterPath(item.redirect||item.path)">{{item.meta.title}}</router-link>
      </el-breadcrumb-item>
    </transition-group>
    <span @click="addViewTags" class="addViewTags" title="锁定页面">
      <svg-icon icon-class="lock"/>
    </span>
    <span @click="reloadPage" class="reloadPage" title="刷新页面">
      <svg-icon icon-class="refresh"/>
    </span>
  </el-breadcrumb>
</template>

<script>
  import Cookies from 'js-cookie'

  export default {
    inject: ['reload'], // 注入 reload 方法
    created() {
      this.getBreadcrumb()
    },
    data() {
      return {
        levelList: null
      }
    },
    watch: {
      $route() {
        this.getBreadcrumb()
      }
    },
    methods: {
      getBreadcrumb() {
        let matched = this.$route.matched.filter(item => item.name)
        const first = matched[0]
        if (first && first.name !== 'dashboard-index') {
          matched = [{ path: '/dashboard', meta: { title: '首页' }}].concat(matched)
        }
        this.levelList = matched
      },
      addViewTags() {
        // 按需缓存，不要全部缓存
        this.$store.dispatch('addVisitedViews', this.$route)
      },
      reloadPage() {
        // 刷新页面
        this.reload()
      },
      setToRouterPath(to) {
        Cookies.set('toRouterPath', to)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 10px;
    .no-redirect {
      color: #ffffff;
      cursor: text;
    }
  }
  .el-breadcrumb__inner a, .el-breadcrumb__inner.is-link{
    color: #ffffff;
  }
  .el-breadcrumb__inner a:hover{
    color: #ffffff;
  }
  .addViewTags {
    margin-left: 10px;
    cursor: Pointer;
    color: #86868d;
  }

  .reloadPage {
    margin-left: 10px;
    cursor: Pointer;
    color: #86868d;
  }
</style>
